home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 8.3 KB | 271 lines | [TEXT/MPS ] |
- // Geometry.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __GEOMETRY__
- #define __GEOMETRY__
-
- // MacApp
-
- #ifndef __MACONDITIONALMACROS__
- #include "MAConditionalMacros.h"
- #endif
-
- // for CCharString types
- #ifndef __PASCALSTRING__
- #include "PascalString.h"
- #endif
-
- // Toolbox
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- //--------------------------------------------------------------------------------
- // Selector operator indicator enumeration. Which coordinate do we want to select? This
- // used to be an enumeration, but we do arithmatic on variables of type VHSelect. This is
- // not legal C++.
- //--------------------------------------------------------------------------------
-
- //typedef unsigned char VHSelect; // use the definition in Types.h
- const VHSelect vSel = 0;
- const VHSelect hSel = 1;
-
-
- //--------------------------------------------------------------------------------
- // CPoint: is a toolbox compatible class for the old struct Point. It is bitwise
- // compatible with the old struct because it contains no virtual functions.
- //--------------------------------------------------------------------------------
-
- typedef struct CPoint *CPointPtr;
-
- class CPoint : public Point
- {
- public:
- // Constructors
-
- inline CPoint() // Default constructor.
- { }
-
- inline CPoint(short iH,
- short iV)
- { v = iV; h = iH; }
-
- inline CPoint(const Point& pt)
- { *this = *(const CPoint*)&pt; }
-
- //------------------------------------------------------------------------------------
- // Copy and conversion operator methods
- //------------------------------------------------------------------------------------
-
- #if qDebug
- // Conversion operator, for converting CPoint to a textual representation of a
- // CPoint. "CPoint(h,v)".
- inline operator const char*() const
- {
- char textPoint[64];
- sprintf (textPoint, "CPoint(%d, %d)", h, v);
- return CChar63(textPoint);
- }
- #endif
-
- // Conversion operators, for converting a CPoint to a long.
- inline operator Point*()
- { return this; }
-
- inline operator const Point*() const
- { return this; }
-
- inline operator long&()
- { return *((long *) this); }
-
- inline operator const long&() const
- { return *((const long *) this); }
-
- // Selector operators, two are needed one for operating on const Points
- // and the other on non-const Points.
- // Because of inlining, constant sel parameters resolve at compile time.
-
- inline short& operator[](VHSelect sel)
- { return (sel == vSel) ? v : h; }
- // For non-const CPoint
-
- inline const short& operator[](VHSelect sel) const
- { return (sel == vSel) ? v : h; }
- // For const CPoint
-
- // Arithmetic operators
-
- CPoint operator+(const CPoint& pt) const; // c = a + b (a,b and c are Points)
- CPoint operator-(const CPoint& pt) const; // c = a - b (a,b and c are Points)
-
- CPoint& operator+=(const CPoint& pt); // c += a (a and c are points, and
- // a reference to a is returned,
- // so these can be strung together)
- CPoint& operator-=(const CPoint& pt); // c -= a ( ditto )
-
- // Relational operators
-
- Boolean operator!=(const CPoint& pt) const; // a != b
- Boolean operator==(const CPoint& pt) const; // a == b
- Boolean operator>(const CPoint& pt) const; // a > b
- Boolean operator<(const CPoint& pt) const; // a < b
- Boolean operator>=(const CPoint& pt) const; // a >= b
- Boolean operator<=(const CPoint& pt) const; // a <= b
- };
-
- //--------------------------------------------------------------------------------
- // Selector operator indicator enumeration. Which CPoint do we want
- // to select, top-left or bottom-right?
- //--------------------------------------------------------------------------------
-
- enum PointSelector { topLeft, botRight };
-
-
- //--------------------------------------------------------------------------------
- // CRect: is a toolbox compatible class for the old struct Rect. It is bitwise
- // compatible with the old struct because it contains no virtual functions.
- //--------------------------------------------------------------------------------
-
- typedef struct CRect *CRectPtr;
-
- class CRect : public Rect
- {
- public:
- // CRect constructors
-
- inline CRect()
- { }
-
- inline CRect(short iLeft, short iTop, short iRight, short iBottom)
- { top = iTop; left = iLeft; bottom = iBottom; right = iRight; }
-
- inline CRect(const CPoint& topLeftPt, const CPoint& botRightPt)
- {
- *(CPoint*)&this->top = topLeftPt;
- *(CPoint*)&this->bottom = botRightPt;
- }
-
- inline CRect(const Rect& rect)
- { *this = *(const CRect*)▭ }
-
- #if qPowerPC
- // copy constructor
- inline CRect(const CRect& rect)
- { *(double*)this = *(const double*)▭ }
- // cheat, we know that we're eight bytes long
- #endif
-
- //------------------------------------------------------------------------------------
- // Copy and conversion operator methods
- //------------------------------------------------------------------------------------
-
- #if qPowerPC
- inline CRect& operator=(const CRect& rect)
- { *(double*)this = *(const double*)▭ return *this; }
- // cheat, we know that we're eight bytes long
- #endif
-
- #if qDebug
- // Conversion operator, for converting CRect to a textual representation of a
- // CRect. "CRect(left,top,right,bottom)".
-
- inline operator const char*() const
- {
- char textRect[64];
- sprintf (textRect, "CRect(%d, %d, %d, %d)", left, top, right, bottom);
- return CChar63(textRect);
- }
- #endif
-
- inline operator Rect*()
- { return this; }
-
- inline operator const Rect*() const
- { return this; }
-
- // CPoint selectors for CRect. One for operating on const Rects and one
- // for non-const Rects.
-
- inline CPoint& operator[](PointSelector sel) // Selector for non-const Rects
- { return (sel == topLeft) ? (*((CPoint *) &top)) : (*((CPoint *) &bottom)); }
-
- inline const CPoint& operator[](PointSelector sel) const // Selector for const Rects
- { return (sel == topLeft) ? (*((const CPoint *) &top)) : (*((const CPoint *) &bottom)); }
-
- // Operators for adding and subtracting one CRect from to/from another.
-
- CRect operator+(const CRect& rt) const;
- CRect operator-(const CRect& rt) const;
- CRect& operator+=(const CRect& rt);
- CRect& operator-=(const CRect& rt);
-
- // Operators overloaded for adding and subtracting some increment to/from each CPoint
- // along both axis. Very convenient for translating Rects. These take a CPoint and since
- // CPoint has a constructor that takes two shorts the CRect windowRect can be translated
- // 100 pixels in the positive y direction by the statement:
- //
- // windowRect = windowRect + CPoint (0, 100); or
- // windowRect += CPoint (0, 100);
-
- CRect operator+(const CPoint& pt) const;
- CRect operator-(const CPoint& pt) const;
- CRect& operator+=(const CPoint& pt);
- CRect& operator-=(const CPoint& pt);
-
-
- CRect& Inset(const CPoint& delta);
- // Inset a CRect by CPoint.
-
- // Equality operators, other relational operator could be defined such as <. But their
- // meaning is less clear and probably better implemented as methods. For example,
- // aRect < bRect could return true if aRect was inside of bRect, or could return true
- // if the area of aRect was less than the area of bRect.
-
- Boolean operator==(const CRect& rt) const;
- Boolean operator!=(const CRect& rt) const;
-
- // Two simple area operators, the intersection & (bitwise and in C++) and the
- // union | (bitwise or in C++). The definition of union here is to return a CRect
- // that exactly encloses its operands.
-
- CRect operator&(const CRect&) const;
- CRect operator|(const CRect& rt) const;
-
- // Returns true if a valid rectangle (left < right and top < bottom). If not
- // a valid rectangle then return false and set all coordinates to 0.
-
- Boolean Empty() const;
-
- Boolean Valid() const;
- // Returns true if a valid rectangle (left < right and top < bottom).
-
- void Validate();
- // Validate rectagle by switching the points if right-bottom is above or to the
- // left of left-top.
-
- // Returns the length of a CRect in a given dimension.
-
- short GetLength(VHSelect sel) const;
-
- // Returns the size of a CRect in both dimensions. (a CPoint)
-
- CPoint GetSize() const;
-
- // The Contains method takes either a CPoint or a CRect and returns true if the operand
- // is inside of the CRect the method is applied to.
-
- Boolean Contains(const CPoint& pt) const;
- Boolean Contains(const CRect& rt) const;
-
- protected:
-
- inline short Min(const short a, const short b) const
- { return a < b ? a : b; }
-
- inline short Max(const short a, const short b) const
- { return a > b ? a : b; }
- };
-
- #endif
-